package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.*;
import com.example.shengyin.service.SymtypeService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.List;

@Validated
@RestController
@RequestMapping("/symtype")
public class SymtypeController {
    @Autowired
    private SymtypeService service;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("分页查询")
    @GetMapping("/list")
    public Result list(Page page,HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        List<Symtype> list = service.list(page);
        int count = service.count();
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }

    @ApiOperation("添加症状")
    @PostMapping("/add")
    public Result insert(@Valid Symtype symtype, BindingResult bindingResult, HttpServletRequest request)
    {

        System.out.println("========================");
        String token = request.getHeader("Authorization");
        System.out.println("========================");
        System.out.println(token);
        if (token==null){
            return Result.error("请登录");
        }
        Admin user = (Admin) redisTemplate.opsForValue().get(token);
        symtype.setPoster(user.getUname());
        int insert = service.insert(symtype);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Result edit(@Valid Symtype symtype,HttpServletRequest request)
    {

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if (admin==null)
            return Result.error("管理员请登录");
        symtype.setPoster(admin.getUname());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        symtype.setPosttime(timestamp);
        int update = service.update(symtype);
        if(update==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }

    @ApiOperation("删除单个")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id,HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }

        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }


    @ApiOperation("传入id查找具体的symtype")
    @GetMapping("/select/{id}")
    public Result select(@PathVariable Long id,HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }

        Symtype symtype = service.select(id);
        return symtype==null?Result.error("修改失败"):Result.success(symtype);
    }

    @ApiOperation("删除多个")
    @DeleteMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids,HttpServletRequest request)
    {
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }


        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

    @ApiOperation("修改状态按钮")
    @GetMapping("/isvalue/{id}/{isvalue}")
    public Result updateIsvalid(@PathVariable(value = "id")Long id,@PathVariable(value = "isvalue")Boolean isvalue,HttpServletRequest request){

        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        if(admin.getRoles()!=1)
        {
            return Result.error("您的权限不足");
        }

        int symtype = service.updateIsvalid(isvalue, id);
        if (symtype>0) {
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

}
